{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to DataFrames\n",
    "**[Bogumił Kamiński](http://bogumilkaminski.pl/about/), May 23, 2018**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using DataFrames # load package"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manipulating columns of DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Renaming columns\n",
    "\n",
    "Let's start with a `DataFrame` of `Bool`s that has default column names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ x1    │ x2    │ x3    │ x4    │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame(Bool, 3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `rename`, we create new `DataFrame`; here we rename the column `:x1` to `:A`. (`rename` also accepts collections of Pairs.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ A     │ x2    │ x3    │ x4    │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rename(x, :x1 => :A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `rename!` we do an in place transformation. \n",
    "\n",
    "This time we've applied a function to every column name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1x1</th><th>x2x2</th><th>x3x3</th><th>x4x4</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ x1x1  │ x2x2  │ x3x3  │ x4x4  │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rename!(c -> Symbol(string(c)^2), x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also change the name of a particular column without knowing the original.\n",
    "\n",
    "Here we change the name of the third column, creating a new `DataFrame`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1x1</th><th>x2x2</th><th>third</th><th>x4x4</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ x1x1  │ x2x2  │ third │ x4x4  │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rename(x, names(x)[3] => :third)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `names!`, we can change the names of all variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>c</th><th>d</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ a     │ b     │ c     │ d     │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names!(x, [:a, :b, :c, :d])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We get an error when we try to provide duplicate names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "LoadError",
     "evalue": "\u001b[91mArgumentError: Duplicate variable names: Symbol[:a, :a, :a, :a].\nPass makeunique=true to make them unique using a suffix automatically.\u001b[39m",
     "output_type": "error",
     "traceback": [
      "\u001b[91mArgumentError: Duplicate variable names: Symbol[:a, :a, :a, :a].\nPass makeunique=true to make them unique using a suffix automatically.\u001b[39m",
      "",
      "Stacktrace:",
      " [1] \u001b[1m#names!#7\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Bool, ::Bool, ::Function, ::DataFrames.Index, ::Array{Symbol,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1mD:\\Software\\JULIA_PKG\\v0.6\\DataFrames\\src\\other\\index.jl:34\u001b[22m\u001b[22m",
      " [2] \u001b[1m(::DataFrames.#kw##names!)\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Array{Any,1}, ::DataFrames.#names!, ::DataFrames.Index, ::Array{Symbol,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m.\\<missing>:0\u001b[22m\u001b[22m",
      " [3] \u001b[1m#names!#19\u001b[22m\u001b[22m at \u001b[1mD:\\Software\\JULIA_PKG\\v0.6\\DataFrames\\src\\abstractdataframe\\abstractdataframe.jl:137\u001b[22m\u001b[22m [inlined]",
      " [4] \u001b[1mnames!\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::DataFrames.DataFrame, ::Array{Symbol,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1mD:\\Software\\JULIA_PKG\\v0.6\\DataFrames\\src\\abstractdataframe\\abstractdataframe.jl:134\u001b[22m\u001b[22m"
     ]
    }
   ],
   "source": [
    "names!(x, fill(:a, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " unless we pass `makeunique=true`, which allows us to handle duplicates in passed names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>a_1</th><th>a_2</th><th>a_3</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ a     │ a_1   │ a_2   │ a_3   │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names!(x, fill(:a, 4), makeunique=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Reordering columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can reorder the names(x) vector as needed, creating a new DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a_1</th><th>a_3</th><th>a_2</th><th>a</th></tr></thead><tbody><tr><th>1</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>2</th><td>false</td><td>false</td><td>false</td><td>false</td></tr><tr><th>3</th><td>false</td><td>false</td><td>false</td><td>false</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ a_1   │ a_3   │ a_2   │ a     │\n",
       "├─────┼───────┼───────┼───────┼───────┤\n",
       "│ 1   │ false │ false │ false │ false │\n",
       "│ 2   │ false │ false │ false │ false │\n",
       "│ 3   │ false │ false │ false │ false │"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1234)\n",
    "x[shuffle(names(x))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "also `permutecols!` will be introduced in next release of DataFrames"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Merging/adding columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │\n",
       "├─────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame([(i,j) for i in 1:3, j in 1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `hcat` we can merge two `DataFrame`s. Also [x y] syntax is supported but only when DataFrames have unique column names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x1_1</th><th>x2_1</th><th>x3_1</th><th>x4_1</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×8 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x1_1   │ x2_1   │ x3_1   │ x4_1   │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hcat(x, x, makeunique=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use `hcat` to add a new column; a default name `:x1` will be used for this column, so `makeunique=true` is needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x1_1</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>1</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>2</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>3</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x1_1 │\n",
       "├─────┼────────┼────────┼────────┼────────┼──────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ 1    │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ 2    │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ 3    │"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = hcat(x, [1,2,3], makeunique=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also prepend a vector with `hcat`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x1_1</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>2</td><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>3</td><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1 │ x1_1   │ x2     │ x3     │ x4     │\n",
       "├─────┼────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ 1  │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ 2  │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ 3  │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hcat([1,2,3], x, makeunique=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively you could append a vector with the following syntax. This is a bit more verbose but cleaner."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>A</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>1</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>2</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>3</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ A │\n",
       "├─────┼────────┼────────┼────────┼────────┼───┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ 1 │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ 2 │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ 3 │"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = [x DataFrame(A=[1,2,3])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we do the same but add column `:A` to the front."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>x1</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>2</td><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>3</td><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ A │ x1     │ x2     │ x3     │ x4     │\n",
       "├─────┼───┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ 1 │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ 2 │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ 3 │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = [DataFrame(A=[1,2,3]) x]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A column can also be added in the middle. Here a brute-force method is used and a new DataFrame is created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  20.993 μs (133 allocations: 10.20 KiB)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>A</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>1</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>2</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>3</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ A │ x3     │ x4     │\n",
       "├─────┼────────┼────────┼───┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ 1 │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ 2 │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ 3 │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using BenchmarkTools\n",
    "@btime [$x[1:2] DataFrame(A=[1,2,3]) $x[3:4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could also do this with a specialized in place method `insert!`. Let's add `:newcol` to the `DataFrame` `y`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>newcol</th><th>x1</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>2</td><td>2</td><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>3</td><td>3</td><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×6 DataFrames.DataFrame\n",
       "│ Row │ A │ newcol │ x1     │ x2     │ x3     │ x4     │\n",
       "├─────┼───┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ 1 │ 1      │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ 2 │ 2      │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ 3 │ 3      │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insert!(y, 2, [1,2,3], :newcol)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to insert the same column name several times `makeunique=true` is needed as usual."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>newcol_1</th><th>newcol</th><th>x1</th><th>x2</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>1</td><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>2</td><td>2</td><td>2</td><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>3</td><td>3</td><td>3</td><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×7 DataFrames.DataFrame\n",
       "│ Row │ A │ newcol_1 │ newcol │ x1     │ x2     │ x3     │ x4     │\n",
       "├─────┼───┼──────────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ 1 │ 1        │ 1      │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ 2 │ 2        │ 2      │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ 3 │ 3        │ 3      │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insert!(y, 2, [1,2,3], :newcol, makeunique=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see how much faster it is to insert a column with `insert!` than with `hcat` using `@btime`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  5.598 μs (20 allocations: 1.45 KiB)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>A</th><th>x3</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>1</td><td>(1, 3)</td><td>(1, 4)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>2</td><td>(2, 3)</td><td>(2, 4)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>3</td><td>(3, 3)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ A │ x3     │ x4     │\n",
       "├─────┼────────┼────────┼───┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ 1 │ (1, 3) │ (1, 4) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ 2 │ (2, 3) │ (2, 4) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ 3 │ (3, 3) │ (3, 4) │"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@btime insert!(copy($x), 3, [1,2,3], :A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's use `insert!` to append a column in place,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>A</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>1</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>2</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>3</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ A │\n",
       "├─────┼────────┼────────┼────────┼────────┼───┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ 1 │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ 2 │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ 3 │"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insert!(x, ncol(x)+1, [1,2,3], :A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and to in place prepend a column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>B</th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>A</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>1</td></tr><tr><th>2</th><td>2</td><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>2</td></tr><tr><th>3</th><td>3</td><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>3</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×6 DataFrames.DataFrame\n",
       "│ Row │ B │ x1     │ x2     │ x3     │ x4     │ A │\n",
       "├─────┼───┼────────┼────────┼────────┼────────┼───┤\n",
       "│ 1   │ 1 │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ 1 │\n",
       "│ 2   │ 2 │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ 2 │\n",
       "│ 3   │ 3 │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ 3 │"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insert!(x, 1, [1,2,3], :B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `merge!`, let's merge the second DataFrame into first, but overwriting duplicates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3×4 DataFrames.DataFrame\n",
       "│ Row │ x   │ y │ z   │ new │\n",
       "├─────┼─────┼───┼─────┼─────┤\n",
       "│ 1   │ 'a' │ 4 │ 'd' │ 11  │\n",
       "│ 2   │ 'b' │ 5 │ 'e' │ 12  │\n",
       "│ 3   │ 'c' │ 6 │ 'f' │ 13  │, 3×3 DataFrames.DataFrame\n",
       "│ Row │ x   │ z   │ new │\n",
       "├─────┼─────┼─────┼─────┤\n",
       "│ 1   │ 'a' │ 'd' │ 11  │\n",
       "│ 2   │ 'b' │ 'e' │ 12  │\n",
       "│ 3   │ 'c' │ 'f' │ 13  │, 3×4 DataFrames.DataFrame\n",
       "│ Row │ x   │ y │ z   │ new │\n",
       "├─────┼─────┼───┼─────┼─────┤\n",
       "│ 1   │ 'a' │ 4 │ 'd' │ 11  │\n",
       "│ 2   │ 'b' │ 5 │ 'e' │ 12  │\n",
       "│ 3   │ 'c' │ 6 │ 'f' │ 13  │)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = DataFrame(x=1:3, y=4:6)\n",
    "df2 = DataFrame(x='a':'c', z = 'd':'f', new=11:13)\n",
    "df1, df2, merge!(df1, df2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " For comparison: merge two `DataFrames`s but renaming duplicate names via `hcat`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x</th><th>y</th><th>x_1</th><th>z</th><th>new</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>4</td><td>'a'</td><td>'d'</td><td>11</td></tr><tr><th>2</th><td>2</td><td>5</td><td>'b'</td><td>'e'</td><td>12</td></tr><tr><th>3</th><td>3</td><td>6</td><td>'c'</td><td>'f'</td><td>13</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x │ y │ x_1 │ z   │ new │\n",
       "├─────┼───┼───┼─────┼─────┼─────┤\n",
       "│ 1   │ 1 │ 4 │ 'a' │ 'd' │ 11  │\n",
       "│ 2   │ 2 │ 5 │ 'b' │ 'e' │ 12  │\n",
       "│ 3   │ 3 │ 6 │ 'c' │ 'f' │ 13  │"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = DataFrame(x=1:3, y=4:6)\n",
    "df2 = DataFrame(x='a':'c', z = 'd':'f', new=11:13)\n",
    "hcat(df1, df2, makeunique=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Subsetting/removing columns\n",
    "\n",
    "Let's create a new `DataFrame` `x` and show a few ways to create DataFrames with a subset of `x`'s columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 5)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 5)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 5)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame([(i,j) for i in 1:3, j in 1:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we could do this by index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 4)</td><td>(1, 5)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 4)</td><td>(2, 5)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 4)</td><td>(3, 5)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×4 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 4) │ (3, 5) │"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[[1,2,4,5]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or by column name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x4</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 4)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 4)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 4)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×2 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x4     │\n",
       "├─────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 4) │\n",
       "│ 2   │ (2, 1) │ (2, 4) │\n",
       "│ 3   │ (3, 1) │ (3, 4) │"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[[:x1, :x4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also choose to keep or exclude columns by `Bool`. (We need a vector whose length is the number of columns in the original `DataFrame`.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x3</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 3)</td><td>(1, 5)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 3)</td><td>(2, 5)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 3)</td><td>(3, 5)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×3 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x3     │ x5     │\n",
       "├─────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 3) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 3) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 3) │ (3, 5) │"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[[true, false, true, false, true]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we create a single column `DataFrame`,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td></tr><tr><th>2</th><td>(2, 1)</td></tr><tr><th>3</th><td>(3, 1)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×1 DataFrames.DataFrame\n",
       "│ Row │ x1     │\n",
       "├─────┼────────┤\n",
       "│ 1   │ (1, 1) │\n",
       "│ 2   │ (2, 1) │\n",
       "│ 3   │ (3, 1) │"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[[:x1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and here we access the vector contained in column `:x1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Tuple{Int64,Int64},1}:\n",
       " (1, 1)\n",
       " (2, 1)\n",
       " (3, 1)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:x1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could grab the same vector by column number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Tuple{Int64,Int64},1}:\n",
       " (1, 1)\n",
       " (2, 1)\n",
       " (3, 1)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and remove everything from a `DataFrame` with `empty!`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th></tr></thead><tbody></tbody></table>"
      ],
      "text/plain": [
       "0×0 DataFrames.DataFrame\n"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empty!(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we create a copy of `x` and delete the 3rd column from the copy with `delete!`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │, 3×4 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 4) │ (3, 5) │)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = copy(x)\n",
    "x, delete!(z, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Modify column by name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 5)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 5)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 5)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame([(i,j) for i in 1:3, j in 1:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With the following syntax, the existing column is modified without performing any copying."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>(1, 2)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 5)</td></tr><tr><th>2</th><td>(2, 2)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 5)</td></tr><tr><th>3</th><td>(3, 2)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 5)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 2) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 2) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 2) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:x1] = x[:x2]\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use the following syntax to add a new column at the end of a `DataFrame`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th><th>A</th></tr></thead><tbody><tr><th>1</th><td>(1, 2)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 5)</td><td>1</td></tr><tr><th>2</th><td>(2, 2)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 5)</td><td>2</td></tr><tr><th>3</th><td>(3, 2)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 5)</td><td>3</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×6 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │ A │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┼───┤\n",
       "│ 1   │ (1, 2) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │ 1 │\n",
       "│ 2   │ (2, 2) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │ 2 │\n",
       "│ 3   │ (3, 2) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │ 3 │"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:A] = [1,2,3]\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A new column name will be added to our `DataFrame` with the following syntax as well (7 is equal to `ncol(x)+1`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th><th>A</th><th>x7</th></tr></thead><tbody><tr><th>1</th><td>(1, 2)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 5)</td><td>1</td><td>11</td></tr><tr><th>2</th><td>(2, 2)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 5)</td><td>2</td><td>12</td></tr><tr><th>3</th><td>(3, 2)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 5)</td><td>3</td><td>13</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×7 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │ A │ x7 │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┼───┼────┤\n",
       "│ 1   │ (1, 2) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │ 1 │ 11 │\n",
       "│ 2   │ (2, 2) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │ 2 │ 12 │\n",
       "│ 3   │ (3, 2) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │ 3 │ 13 │"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[7] = 11:13\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find column name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>(1, 1)</td><td>(1, 2)</td><td>(1, 3)</td><td>(1, 4)</td><td>(1, 5)</td></tr><tr><th>2</th><td>(2, 1)</td><td>(2, 2)</td><td>(2, 3)</td><td>(2, 4)</td><td>(2, 5)</td></tr><tr><th>3</th><td>(3, 1)</td><td>(3, 2)</td><td>(3, 3)</td><td>(3, 4)</td><td>(3, 5)</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1     │ x2     │ x3     │ x4     │ x5     │\n",
       "├─────┼────────┼────────┼────────┼────────┼────────┤\n",
       "│ 1   │ (1, 1) │ (1, 2) │ (1, 3) │ (1, 4) │ (1, 5) │\n",
       "│ 2   │ (2, 1) │ (2, 2) │ (2, 3) │ (2, 4) │ (2, 5) │\n",
       "│ 3   │ (3, 1) │ (3, 2) │ (3, 3) │ (3, 4) │ (3, 5) │"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame([(i,j) for i in 1:3, j in 1:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can check if a column with a given name exists via"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ":x1 in names(x) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and determine its index via"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "findfirst(names(x), :x2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
